<?php
/**
 * Mechanism
 *
 * LICENSE
 *
 * Copyright (c) 2011, Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following disclaimer
 *   in the documentation and/or other materials provided with the
 *   distribution.
 * * Neither the name of the  nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package Mechanism
 * @license http://www.opensource.org/licenses/bsd-license
 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * @copyright Copyright (c) 2011 Sergey A. Ivonchik
 */

namespace Mechanism\Router\Route {
	/**
	 * Composite route
	 *
	 * This is composite for routes. Using this class
	 * we can create composite routes which includes several
	 * parts f.e. Uri and Host.
	 *
 	 * @package Mechanism\Router\Route
 	 * @author BreatheInMyVoid <BreatheInMyVoid@gmail.com>
	 */
	class Composite implements IRoute {
		/**
		 * Routes
		 *
		 * This is routes parts for url matching.
		 * This is array in which key it is name of route
		 * part class and value it is route object.
		 * By this you can add only one instance of
		 * specific route part ( f.e. only one Uri instance )
		 * if you add more then one it will be overwritten.
		 *
		 * Example:
		 *     $routes = array(
		 *         'Host' => $hostRoute,
		 *         'Uri'  => $uriRoute
		 *     );
		 *
		 * @var array $routes
		 */
		protected $routes = array( );

		/**
		 * Parameters of route parts
		 *
		 * @var array $params
		 */
		protected $params = array( );

		/**
		 * Add route part for url matching
		 *
		 * @param IRoute $route
		 * @return Composite $this
		 */
		public function add( IRoute $route ) {
			$name = substr( strrchr( get_class( $route ), '\\' ), 1 );
			$this->routes[$name] = $route;

			return $this;
		}

		/**
		 * Remove route part
		 *
		 * @param string $name
		 * @return Composite $this
		 */
		public function remove( $name ) {
			if ( isset( $this->routes[$name] ) && !empty( $this->routes[$name] ) ) {
				unset( $this->routes[$name] );
			} else {
				throw new Exception\Invalid( 
					'Can not remove '
					. $name
					. ' route from Composite. Route not has been added.'
				);
			}

			return $this;
		}

		/**
		 * Set parameter of a route
		 *
		 * This method sets special parameter for
		 * a route f.e. module, controller, action
		 * and etc.
		 *
		 * @see IRoute
		 * @param string $name
		 * @param string $value
		 */
		public function set( $name, $value ) {
			$this->params[$name] = $value;

			return $this;
		}

		/**
		 * Get parameter of a route
		 *
		 * This method gets special parameter of a route.
		 * Do not set same named parameters in your routes!
		 * Collapses possible!
		 *
		 * @see IRoute
		 * @param string $name = null
		 * @return string $param
		 * @return array $params
		 */
		public function get( $name = null ) {
			/* Get all params if name is null */
			if ( null === $name ) {
				return $this->params;
			} else {
				/* Look in local params if name is setted */
				if ( array_key_exists( $name, $this->params ) ) {
					return $this->params[$name];
				} else {
					/* Look in routes */
					$value = null;

					foreach( $this->routes as $route ) {
						if ( null !== $route->get( $name ) ) {
							return $route->get( $name );
						}
					}

					return $value;
				}
			}
		}

		/**
		 * Match path
		 *
		 * This method matches given path using added routes.
		 * User should pass full url! Passing uri or url without
		 * scheme returns wrong result because of PHP parse_url function
		 * expects full url.
		 * 
		 * @param string $path
		 * @return bool $result
		 */
		public function match( $path ) {
			/* Decode raw url */
			$path = rawurldecode( $path );

			/* Match every route */
			foreach( $this->routes as $route ) {
				if ( false === $route->match( $path ) ) {
					return false;
				} else {
					/* Get params of route */
					$this->params = array_merge( $this->params, $route->get( ) );
				}
			}
			
			return true;
		}		
	}
}